//
//  GPSExerciseHeartZoneTableViewCell.swift
//  DaFit
//
//  Created by Paul on 2022/10/27.
//  Copyright © 2022 sylar. All rights reserved.
//

import UIKit
import MYModule

open class GPSTrainingHeartZoneTableViewCell: TableViewCell {
    
    open var hearts: [Int]? {
        
        didSet {
            
            let heartRates = hearts ?? []
            
           let heartRateZoneModel = HeartRateZoneModel.calculationMinute(with: heartRates, heartRateInterval: 10, componet: .second)
            zoneView.bind(to: heartRateZoneModel)
            
        }
        
    }
    
//    var times: [Int]? {
//
//        didSet {
//
//            guard let times = times else {
//                return
//            }
//
//            var totalSecond = 0
//
//            for (i, time) in times.enumerated() {
//                if i < heartRateZoneModel.subModels.count {
//                    let subModel = heartRateZoneModel.subModels[i]
//                    if time > 0 {
//                        totalSecond += time
//                    }
//                    subModel.second = time
//                }
//            }
//
//            heartRateZoneModel.totalSecond = totalSecond
//
//            zoneView.bind(to: heartRateZoneModel)
//        }
//
//    }
    
//    private let heartRateZoneModel = HeartRateZoneModel()
//
    private lazy var zoneView: HeartRateZoneView = {
        let view = HeartRateZoneView()
        view.hiddemDendescription = true
        view.setRoundedCorners(.all, radius: 18)
        return view
    }()
    
    open override func makeUI() {
        super.makeUI()
        
        contentView.backgroundColor = UIColor.darkBackground
        
        self.zoneView.backgroundColor = UIColor.background
        self.zoneView.descriptionView.updateData(textColor: UIColor.HeartRate.theme, title: "heart_rate_zone_description_title".language, explain: "heart_rate_zone_description_content".language,
                                                 explainTextColor: UIColor.HeartRate.theme.withAlphaComponent(0.8),
                                                 bgColor: UIColor.HeartRate.theme)
        
        
        contentView.addSubview(zoneView)
        
        zoneView.snp.makeConstraints { make in
            make.trailing.leading.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
    }
    
}

open class GPSTrainingHeartRateZoneView: View{
    
    public let titleVerticalStackView = TitleStackView()
    public let timeVerticalStackView = TitleStackView()
    
    public let barView = BarStackView()
    
    open var times: [Int] = []{
        didSet{
            if times != oldValue{
                setBarView()
            }
        }
    }
    
    open override func makeUI(){
        
        titleVerticalStackView.axis = .vertical
        titleVerticalStackView.alignment = .trailing
        titleVerticalStackView.textColor = UIColor.assist_222222
        
        titleVerticalStackView.titles = [
            "heart_rate_light".language,
            "heart_rate_weight".language,
            "heart_rate_aerobic".language,
            "heart_rate_anaerobic".language,
            "heart_rate_max".language
        ]
        
        timeVerticalStackView.axis = .vertical
        timeVerticalStackView.alignment = .leading
        timeVerticalStackView.textColor = UIColor.assist_222222
        timeVerticalStackView.titles = ["--","--","--","--","--"]
        self.addSubview(barView)
        self.addSubview(titleVerticalStackView)
        self.addSubview(timeVerticalStackView)
        
        barView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(72)
            make.right.equalToSuperview().offset(-76)
            make.top.bottom.equalToSuperview()
        }
        
        titleVerticalStackView.snp.makeConstraints { make in
            make.right.equalTo(barView.snp.left).offset(-5)
            make.top.equalTo(barView.snp.top)
            make.bottom.equalTo(barView.snp.bottom)
        }
        
        timeVerticalStackView.snp.makeConstraints { make in
            make.left.equalTo(barView.snp.right).offset(5)
            make.top.equalTo(barView.snp.top)
            make.bottom.equalTo(barView.snp.bottom)
        }
    }
    
    open func setBarView(){
        var totalTime = 0
        var timeStr:[String] = []
        for i in times{
            var str = "home_dyheart_sub_time".language
            str = str.replacingOccurrences(of: "$mm", with: String(i))
            timeStr.append(str)
            totalTime += i
        }
        timeVerticalStackView.titles = timeStr
        var progress:[Double] = []
        for value in times{
            progress.append(Double(value)/Double(totalTime))
        }
        barView.values = progress
    }
    
    open class BarStackView: UIStackView {
        
        public var barViews: [UIView] = []
        
        open var values: [Double] = []{
            didSet {
                
                if values == oldValue {
                    return
                }
                if values.count != barViews.count{
                    return
                }
                for i in 0..<barViews.count{
                    let progressView = barViews[i]
                    let value = values[i]
                    setProgress(progressView: progressView, tag: i, progress: value)
                }
            }

        }
        
        public required init() {
            super.init(frame: CGRect.zero)
            makeUI()
        }
        
        public required init(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        open func makeUI() {
            for i in 0..<5 {
                let view = UIView()
                view.backgroundColor = UIColor.darkBackground
                self.addArrangedSubview(view)
                view.layer.cornerRadius = 2
                view.layer.masksToBounds = true
                self.barViews.append(view)
            }
            axis = .vertical
            distribution = .fillEqually
            alignment = .fill
            spacing = 5
        }
        
        open func setProgress(progressView: UIView, tag: Int, progress: Double){
            progressView.subviews.forEach { (view) in
                view.removeFromSuperview()
            }
            
            if (progress.isNaN || progress == 0.0) {
                return
            }
            
            var strokeColor = UIColor.clear
            switch tag {
            case 0:
                strokeColor = UIColor(red: 255/255, green: 224/255, blue: 97/255, alpha: 1)
            case 1:
                strokeColor = UIColor(red: 255/255, green: 197/255, blue: 102/255, alpha: 1)
            case 2:
                strokeColor = UIColor(red: 253/255, green: 168/255, blue: 103/255, alpha: 1)
            case 3:
                strokeColor = UIColor(red: 250/255, green: 141/255, blue: 105/255, alpha: 1)
            case 4:
                strokeColor = UIColor(red: 242/255, green: 120/255, blue: 109/255, alpha: 1)
            default:
                break
            }
            let width = progressView.width * progress
            let barView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: width, height: progressView.height))
            barView.layer.cornerRadius = 2
            barView.layer.masksToBounds = true
            barView.backgroundColor = strokeColor
            progressView.addSubview(barView)
            barView.snp.makeConstraints { make in
                make.left.height.top.bottom.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(progress)
            }
        }
    }
    
}
